package com.mission.schedule.tools.toexcel

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.Toast
import com.mission.schedule.R
import com.mission.schedule.activity.BaseActivity
import com.mission.schedule.applcation.App
import com.mission.schedule.bean.qd606.NoteTitleDetailBean
import com.mission.schedule.constants.ShareFile
import com.mission.schedule.email.SendMailUtil
import com.mission.schedule.utils.SharedPrefUtil
import kotlinx.android.synthetic.main.commonreply.view.*
import kotlinx.android.synthetic.main.to_excel.*
import java.io.File

class ToExcelActivity: BaseActivity() {

    internal var userID = ""
    var file: File? = null
    var email_type = 0
    override fun init(savedInstanceState: Bundle?) {
        setContentView(R.layout.to_excel)
    }

    override fun setContentView() {
        val sharedPrefUtil = SharedPrefUtil(this, ShareFile.USERFILE)
        userID = sharedPrefUtil.getString(this, ShareFile.USERFILE, ShareFile.USERID, "0")
    }

    override fun setListener() {
        exit.setOnClickListener {
            finish()
        }

        d0.setOnClickListener {
            var title = arrayListOf("日程", "重复","我的清单", "我的归档") as List<String>
            val list1 = App.getDBcApplication().getFiledNoteTitlesData(userID, "0")
            val list2 = App.getDBcApplication().getFiledNoteTitlesData(userID, "1")
            var lists = ArrayList<List<NoteTitleDetailBean.TDelListBean>>()
            lists.add(list1)
            lists.add(list2)

            val calender = App.getDBcApplication().queryAllSchData(-4,0,0)
            val repeat = App.getDBcApplication().QueryAllChongFuData(1)
            var maps = ArrayList<List<Map<String, String>>>()
            maps.add(calender)
            maps.add(repeat)
            file = ExcelUtil.WriteExcel(this, 0, maps, title, lists, "time_sheet", ischild.isChecked, userID)
        }

        d1.setOnClickListener {
           c_r()
        }

        d2.setOnClickListener {
           note()
        }

        toemail.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                email_type = position
            }
        }

        send.setOnClickListener {

            val send = SendMailUtil.send(file, toadd.text.toString(),
                    if (email_type == 0) SendMailUtil.HostPort.QQ else SendMailUtil.HostPort.SINA,
                    add.text.toString(), pwd.text.toString())

            Toast.makeText(this, if (send) {
                "发送成功"
            } else {
                "发送失败"
            }, Toast.LENGTH_LONG).show()
        }
    }

    fun note() {
        var title = arrayListOf("我的清单", "我的归档") as List<String>
        val list1 = App.getDBcApplication().getFiledNoteTitlesData(userID, "0")
        val list2 = App.getDBcApplication().getFiledNoteTitlesData(userID, "1")
        var lists = ArrayList<List<NoteTitleDetailBean.TDelListBean>>()
        lists.add(list1)
        lists.add(list2)
        file = ExcelUtil.WriteExcel(this, 2, null, title, lists, "note", ischild.isChecked, userID)
    }

    fun c_r() {
        var title = arrayListOf("日程", "重复") as List<String>
        val calender = App.getDBcApplication().queryAllSchData(-4,0,0)
        val repeat = App.getDBcApplication().QueryAllChongFuData(1)
        var maps = ArrayList<List<Map<String, String>>>()
        maps.add(calender)
        maps.add(repeat)
        file = ExcelUtil.WriteExcel(this, 1, maps, title, null, "calender_repeat", ischild.isChecked, userID)
    }

    override fun setAdapter() {
    }
}